#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <iostream>
#include "vplatform.h"

#include "vocon3200_base.h"
#include "vocon3200_asr.h"
#include "vocon3200_sem.h"
#include "vocon3200_pron.h"
#include "vocon3200_gram2.h"
#include "vocon3200_sse.h"
#include "vocon_ext_heap.h"
#include "vocon_ext_stream.h"
#include "vocon_ext_audioin.h"
#include "vocon_ext_asr2sem.h"

#include "UILogger.h"
#include "UIConfigResourceManager.h"
#include "UIEventRouter.h"
#include "UISpeechEvent.h"
#include "UISpeechManager.h"
//#include "UILanguageInterpreter.h"
//#include "UIContextManager.h"

int main( int argc, char **argv )
{
	UILogger::create(LOGGER_LOG4CXX, "../Common/Resources/Logging/logging-log4cxx.config");
	UIConfigResourceManager::create("../Common/Resources/main-TestSR.config", "./usersetting.config");

	UISpeechManager speech;
//	UILanguageInterpreter nlu;
//	UIContextManager cm;

	UIEvent eventRecogFile;
	eventRecogFile.setSender(MODULE_DialogueManager);
	eventRecogFile.setReciever(MODULE_SpeechManager);
	eventRecogFile.setType(EVENT_IN_USER_VOICE_DATA);
		
	UIEvent eventRecogMic;
	eventRecogMic.setSender(MODULE_DialogueManager);
	eventRecogMic.setReciever(MODULE_SpeechManager);
	eventRecogMic.setType(EVENT_IN_START_RECORDING);

	UIEvent eventTTS;
	eventTTS.setSender(MODULE_DialogueManager);
	eventTTS.setReciever(MODULE_SpeechManager);
	eventTTS.setType(EVENT_IN_TTS_TEXT);

	UIEvent eventContextUpdateDictation;
	eventContextUpdateDictation.setSender(MODULE_ContextManager);
	eventContextUpdateDictation.setReciever(MODULE_SpeechManager);
	eventContextUpdateDictation.setType(EVENT_IN_CONTEXT_UPDATE);
	eventContextUpdateDictation.setValue("lastSysQuestion=WHQuestion:SocialContent");

	UIEvent eventContextUpdateDictationDone;
	eventContextUpdateDictationDone.setSender(MODULE_ContextManager);
	eventContextUpdateDictationDone.setReciever(MODULE_SpeechManager);
	eventContextUpdateDictationDone.setType(EVENT_IN_CONTEXT_UPDATE);
	eventContextUpdateDictationDone.setValue("lastSysQuestion=EMPTY");

	UIEvent eventContextUpdateNetworkDisconnected;
	eventContextUpdateNetworkDisconnected.setSender(MODULE_ContextManager);
	eventContextUpdateNetworkDisconnected.setReciever(MODULE_SpeechManager);
	eventContextUpdateNetworkDisconnected.setType(EVENT_IN_CONTEXT_UPDATE);
	eventContextUpdateNetworkDisconnected.setValue("networkStatus=disconnected");

	UIEvent eventContextUpdateNetworkConnected;
	eventContextUpdateNetworkConnected.setSender(MODULE_ContextManager);
	eventContextUpdateNetworkConnected.setReciever(MODULE_SpeechManager);
	eventContextUpdateNetworkConnected.setType(EVENT_IN_CONTEXT_UPDATE);
	eventContextUpdateNetworkConnected.setValue("networkStatus=connected");

	UIEvent eventBatchSR;
	eventBatchSR.setSender(MODULE_DialogueManager);
	eventBatchSR.setReciever(MODULE_SpeechManager);
	eventBatchSR.setType(EVENT_IN_BATCH_SR_TEST);

	UIEvent eventBatchSRNLU;
	eventBatchSRNLU.setSender(MODULE_DialogueManager);
	eventBatchSRNLU.setReciever(MODULE_SpeechManager);
	eventBatchSRNLU.setType(EVENT_IN_BATCH_SR_NLU_TEST);

	UIEvent eventPairTest;
	UIEvent eventPairTestReturned;
	eventPairTest.setSender(MODULE_SpeechManager);
	eventPairTest.setReciever(MODULE_ContextManager);

	char t;
	int result;
	result = scanf("%c", &t);
	if (result) return 1;
	while (t != 'q') {
		switch (t) {
		case 'm':	// test recognize from MIC
			UIEventRouter::getInstance()->sendEvent(&eventRecogMic);
			break;

		case 'f':	// test recognize from FILE
			eventRecogFile.setValue("{\"file\": \"D:\\test.wav\"}");
			UIEventRouter::getInstance()->sendEvent(&eventRecogFile);
			break;	// test tts

		case 't':
			eventTTS.setValue("{\"text\": \"This is a pause=1000=endpause test. HAHAHA\"}");
			UIEventRouter::getInstance()->sendEvent(&eventTTS);
			break;

		case 'd':	// test dictation from MIC
			UIEventRouter::getInstance()->sendEvent(&eventContextUpdateDictation);
			UIEventRouter::getInstance()->sendEvent(&eventRecogMic);
			UIEventRouter::getInstance()->sendEvent(&eventContextUpdateDictationDone);
			break;

		case 'n':	// test no network
			eventRecogFile.setValue("{\"file\": \"D:\\test.wav\"}");
			UIEventRouter::getInstance()->sendEvent(&eventContextUpdateNetworkDisconnected);
			UIEventRouter::getInstance()->sendEvent(&eventRecogFile);
			UIEventRouter::getInstance()->sendEvent(&eventContextUpdateNetworkConnected);
			break;

		case 'b':	// batch test: SR only
			eventBatchSR.setValue("{\"hrl\": \"D:\\GMNGA_Tuning\\LaCrosse.hrl\"}");
			UIEventRouter::getInstance()->sendEvent(&eventBatchSR);
			break;

		case 'u':	// batch test: SR + NLU
			eventBatchSRNLU.setValue("{\"hrl\": \"D:\\GMNGA_Tuning\\LaCrosse.hrl\"}");
			UIEventRouter::getInstance()->sendEvent(&eventBatchSRNLU);
			break;

		case 'p':	// test paired event
			UIEventRouter::getInstance()->sendSyncEvent(&eventPairTest, eventPairTestReturned, 10);
		    std::cout << eventPairTestReturned.serializeJson() << std::endl;
			break;

		default:
			break;
		}
		result = scanf("%c", &t);
		if (result) return 1;
	}
	SRNLUBatchTester::getInstance()->endLogResult();

	UILogger::release();
	UIConfigResourceManager::release();
	UIEventRouter::release();
	SRNLUBatchTester::release();
}
